<?php
namespace app\tag\controller;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminTag extends AdminBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->item = 'Tags';
        $this->itemType = 'tag';
        $this->itemName = '标签';
        $this->itemCategory = 'TagsCategory';
        $this->itemContent = 'TagsContent';
        $this->itemModelNameSpace = 'app\tag\model\Tags';
        $this->itemCategoryModelNameSpace = 'app\tag\model\TagsCategory';
    }
    
    public function itemAdd()
    {
        $data = $this->request->post();
        if (!isset($data['title']) || !$data['title']) {
            return jsonError('请输入标题');
        }
        $itemInfo = db($this->item)->where('title',$data['title'])->find();
        if ($itemInfo) {
            return jsonError('标题已存在');
        }
        if (isset($data['url_name']) && $data['url_name']) {
            $itemInfoByUrlName = db($this->item)->where('url_name',$data['url_name'])->find();
            if ($itemInfoByUrlName) {
                return jsonError('自定义的Url已存在');
            }
        }
        $fieldList = input('post.fieldList');
        $fieldList = json_decode($fieldList,true);
        $res = model($this->itemModelNameSpace)->itemAdd($data,$fieldList);
        if ($res) {
            return jsonSuccess('操作成功');
        } else {
            return jsonError('操作失败');
        }
    }
    
    
    public function txtAdd(Request $request)
    {
        $file = $request->file('fileDataFileName');
        $cid = input('post.cid');
        $siteId = input('post.siteId');
        if ($siteId) {
            config('dataId',$siteId);
        }
        if (empty($file)) {
            return jsonError('文本不存在');
        }
        $tempFile = $file->getInfo();
        $filePath = $tempFile['tmp_name'];
        $allList = fopen($filePath, "r");
        $tempList = [];
        $i = 0;
        while(!feof($allList))
        {
            $tempList[$i]= fgets($allList);
            $i++;
        }
        fclose($allList);
        $content = '';
        $data['cid'] = $cid;
        foreach ($tempList as $key => $value) {
        	$data['title'] = trim($value);
        	$res = model($this->itemModelNameSpace)->itemAdd($data,$fieldList);
        }
     	return jsonSuccess('操作成功');
    }
    
    public function itemEdit()
    {
		$data = $this->request->post();
        if (!isset($data['title']) || !$data['title']) {
            return jsonError('请输入标题');
        }
        $itemInfo = db($this->item)->where('id',$data['id'])->find();
        if (!$itemInfo) {
            return jsonError('不存在');
        }
        $itemInfo = db($this->item)->where('id','<>',$data['id'])->where('title',$data['title'])->find();
        if ($itemInfo) {
            return jsonError('标题已存在');
        }
        if (isset($data['url_name']) && $data['url_name']) {
            $itemInfoByUrlName = db($this->item)->where('id','<>',$data['id'])->where('url_name',$data['url_name'])->find();
            if ($itemInfoByUrlName) {
                return jsonError('自定义的Url已存在');
            }
        }
        $fieldList = input('post.fieldList');
        $fieldList = json_decode($fieldList,true);
        $res = model($this->itemModelNameSpace)->itemEdit($data,$fieldList);
        if ($res) {
            return jsonSuccess('操作成功');
        } else {
            return jsonError('操作失败');
        }
    }
	
    public function itemDel()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = db($this->item)->where('id',$id)->find();
        if (!$itemInfo) {
            return jsonError('删除项不存在');
        }
        $res = model($this->itemModelNameSpace)->itemDel($id);
        if ($res) {
            return jsonSuccess('操作成功');
        } else {
            return jsonError('操作失败');
        }
    }

    public function itemsDel()
    {
        $ids = input('post.ids');
        if (!$ids) {
          return jsonError('缺少参数');
        }
        $ids = explode(',',$ids);
        foreach ($ids as $id) {
            model($this->itemModelNameSpace)->itemDel($id);
        }
        return jsonSuccess('操作成功');
    }
    
    public function itemTransferAll()
    {
        $cid = input('post.cid');
        $ids = input('post.ids');
        if (!$ids) {
          return jsonError('缺少参数');
        }
        if ($cid == '') {
            $cid = 0;
        }
        $ids = explode(',',$ids);
        if (is_array($ids)) {
            foreach ($ids as $id){
                db($this->item)->where('id',$id)->update(['cid' => $cid]);
            }
            return jsonSuccess('操作成功');
        } else {
            return  jsonError('参数错误');
        }
    }
    
    public function itemFind()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = model($this->itemModelNameSpace)->getItemInfo(null,$id);
        if (!$itemInfo) {
          return jsonError('不存在');
        }
        return jsonSuccess('',$itemInfo);
    }

    public function itemList()
    {
        $data = $this->request->post();
        $itemList = model($this->itemModelNameSpace)->getItemList($data);
        $itemCount = model($this->itemModelNameSpace)->getCount($data['cid'], $data['keywords']);
	    return jsonSuccess('',['itemList' => $itemList,'total' => $itemCount,'page' => $data['page']]);
    }
    
    public function import()
    {
        vendor("PHPExcel.PHPExcel");
        vendor("PHPExcel.PHPExcel.Reader.Excel5");
        vendor("PHPExcel.PHPExcel.Reader.Excel2007");
        vendor("PHPExcel.PHPExcel.Reader.CSV");
        vendor("PHPExcel.PHPExcel.IOFactory");
        
        $file = request()->file('fileDataFileName');
        $info = $file->validate(['ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel' . DS);
        if ($info) {
            $exclePath = $info->getSaveName();  
            $fileName = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel' . DS . $exclePath; 
            $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION) );
            
            if ($extension =='xlsx') {
                $objReader = new \PHPExcel_Reader_Excel2007();
                $objExcel = $objReader ->load($fileName);
            } else if ($extension =='xls') {
                $objReader = new \PHPExcel_Reader_Excel5();
                $objExcel = $objReader ->load($fileName);
            } else if ($extension=='csv') {
                $PHPReader = new \PHPExcel_Reader_CSV();
                $PHPReader->setInputEncoding('GBK');
                $PHPReader->setDelimiter(',');
                $objExcel = $PHPReader->load($fileName);
            }
            $excel_array = $objExcel->getSheet(0)->toArray();
            array_shift($excel_array);
            $data = [];
            foreach ($excel_array as $k => $v) {
                $data['title'] = $v[0];
                model($this->itemModelNameSpace)->itemAdd($data);
            }
            return jsonSuccess('操作成功',$excel_array);
        } else {
            return jsonError($file->getError());
        }
        
    }
}